﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.XmlDataModel;
using System.Collections.ObjectModel;
using System.Xml;
using SharedLib.Helpers;

namespace Tracy.DataModel
{
    public class FilterSet:XmlDataModelWithAttributes
    {
        public FilterSet() : base() { }
        public FilterSet(XmlNode node) : base(node) { }

        private SortedSet<FanSubGroup> _fanSubGroups;

        public SortedSet<FanSubGroup> FanSubGroups
        {
            get { if (_fanSubGroups == null)_fanSubGroups = new SortedSet<FanSubGroup>(); return _fanSubGroups; }
            set { _fanSubGroups = value; }
        }

        private Format _format;

        public Format Format
        {
            get { return _format; }
            set {
                Properties["format"] = value.Name;
                _format = value; 
            }
        }

        private Resolution _resolution;

        public Resolution Resolution
        {
            get { return _resolution; }
            set 
            {
                Properties["resolution"] = value.Name;
                _resolution = value; 
            }
        }

        protected override XmlElement GenerateXmlNode(XmlDocument doc)
        {
            XmlElement rootNode = base.GenerateXmlNode(doc);
            if (FanSubGroups.Count > 0)
            {
                XmlElement fanSubGroupsNode = doc.CreateElement("fan-sub-groups");
                foreach (FanSubGroup fanSubGroup in FanSubGroups)
                {
                    fanSubGroupsNode.AppendChild(XmlHelper.CreateElementWithTextValue("fan-sub-group", fanSubGroup.Name, doc));
                }
                rootNode.AppendChild(fanSubGroupsNode);
            }

            return rootNode;
        }

        protected override void ParseRootNode(XmlElement rootNode)
        {
            base.ParseRootNode(rootNode);
            foreach (string fanSubGroupName in XmlHelper.GetNodeValues(rootNode, "fan-sub-groups/fan-sub-group"))
            {
                FanSubGroups.Add(TracyBus.Instance.ConfigurationManager.FanSubGroupProvider.GetItemByName(fanSubGroupName));
            }

            string format = XmlHelper.GetNodeValue(rootNode, "format");
            if (!String.IsNullOrEmpty(format)) Format = TracyBus.Instance.ConfigurationManager.FormatProvider.GetItemByName(format);

            string resolution = XmlHelper.GetNodeValue(rootNode, "resolution");
            if (!String.IsNullOrEmpty(resolution)) Resolution = TracyBus.Instance.ConfigurationManager.ResolutionProvider.GetItemByName(resolution);
        }

        public override string RootNodeName
        {
            get
            {
                return "filter-set";
            }
        }

        public bool Equals(FilterSet anotherFilterSet)
        {
            //TODO: Use ID to compare
            //Compare FanSubGroups
            if (anotherFilterSet.FanSubGroups.Count != FanSubGroups.Count) return false;
            SortedSet<FanSubGroup>.Enumerator e = FanSubGroups.GetEnumerator();
            SortedSet<FanSubGroup>.Enumerator ae = anotherFilterSet.FanSubGroups.GetEnumerator();

            while (e.MoveNext())
            {
                ae.MoveNext();
                if (e.Current.Name != ae.Current.Name) return false;
            }


            //Compare Format
            if (Format.Name != anotherFilterSet.Format.Name) return false;

            //Compare Resolution
            if (Resolution.Name != anotherFilterSet.Resolution.Name) return false;

            return true;
        }
        
        
    }
}
